﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile.Nodes
{
	public class Field : ICodeContainer
	{
		public bool IsStatic { get; private set; }
		public string Name { get; private set; }
		public int NameIndex { get { return Variable.Table.GetId(this.Name); } }
		public Expression DefaultValue { get; private set; }
		public int ID { get; set; }

		private Field(string name, bool isStatic, Expression defaultValue)
		{
			this.Name = name;
			Variable.Table.Register(name);
			this.IsStatic = isStatic;
			this.DefaultValue = defaultValue;
		}

		public static Field Parse(Tokens tokens)
		{
			bool isStatic = tokens.PopIfPresent("static");
			tokens.PopExpected("field");
			string name = tokens.PopIdentifier();

			Expression defaultValue = NullConstant.Instance;
			if (tokens.PopIfPresent("="))
			{
				defaultValue = Expression.Parse(tokens);
			}

			tokens.PopExpected(";");

			return new Field(name, isStatic, defaultValue);
		}

		public void ResolveCode(Class c, Dictionary<object, object> parents)
		{
			this.DefaultValue.ResolveCode(c, parents);
		}

		public void ResolveClassToThis(Class c)
		{
			// No reason to do this.
			// The field default values wil be included in the static initializer which will have this method called upon it.
		}
	}
}
